package com.itbaizhan.shopping_goods_service.config;

/**
 * @author spf
 * @date 2024/1/8
 * @time 16:25
 */

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.swing.plaf.PanelUI;

/**
 * rabbitMQ配置文件
 */

@Configuration
public class RabbitConfig {

    private final String GOODS_EXCHANGE = "goods_exchange";
    private final String SYNC_GOODS_QUEUE = "sync_goods_queue";
    private final String DEL_GOODS_QUEUE = "del_goods_queue";
    private final String SYNC_CART_QUEUE = "sync_cart_queue";
    private final String DEL_CART_QUEUE = "del_cart_queue";

    @Bean(GOODS_EXCHANGE)
    public Exchange getExchange(){
        Exchange exchange = ExchangeBuilder.topicExchange(GOODS_EXCHANGE)
                .durable(true)
                .build();
        return exchange;
    }

    @Bean(SYNC_GOODS_QUEUE)
    public Queue getQueue1(){
        return new Queue(SYNC_GOODS_QUEUE);
    }

    @Bean(DEL_GOODS_QUEUE)
    public Queue getQueue2(){
        return new Queue(DEL_GOODS_QUEUE);
    }

    @Bean(SYNC_CART_QUEUE)
    public Queue getQueue3(){
        return new Queue(SYNC_CART_QUEUE);
    }

    @Bean(DEL_CART_QUEUE)
    public Queue getQueue4(){
        return new Queue(DEL_CART_QUEUE);
    }

    @Bean
    public Binding bindQueue1(@Qualifier(GOODS_EXCHANGE) Exchange exchange,
                              @Qualifier(SYNC_GOODS_QUEUE) Queue queue){
        Binding binding = BindingBuilder.bind(queue)
                .to(exchange)
                .with("#.sync_goods.#")
                .noargs();
        return binding;
    }

    @Bean
    public Binding bindQueue2(@Qualifier(GOODS_EXCHANGE) Exchange exchange,
                              @Qualifier(DEL_GOODS_QUEUE) Queue queue){
        Binding binding = BindingBuilder.bind(queue)
                .to(exchange)
                .with("#.del_goods.#")
                .noargs();
        return binding;
    }

    @Bean
    public Binding bindQueue3(@Qualifier(GOODS_EXCHANGE) Exchange exchange,
                              @Qualifier(SYNC_CART_QUEUE) Queue queue){
        Binding binding = BindingBuilder.bind(queue)
                .to(exchange)
                .with("#.sync_cart.#")
                .noargs();
        return binding;
    }

    @Bean
    public Binding bindQueue4(@Qualifier(GOODS_EXCHANGE) Exchange exchange,
                              @Qualifier(DEL_CART_QUEUE) Queue queue){
        Binding binding = BindingBuilder.bind(queue)
                .to(exchange)
                .with("#.del_cart.#")
                .noargs();
        return binding;
    }

}
